/*
 * @Author: chunlai
 * @Date: 2023-07-25 09:48:14
 * @LastEditTime: 2025-09-17 09:56:22
 * @LastEditors: minsheng_insurance_agency_zhoucl
 * @FilePath: \02.bldinsure\src\store\modules\apis.js
 * @Description: 公共接口
 */
import $http from "@/utils/request";
import { GetToRateData } from "@/translate/risks/rate";
const state = {
  dicStoreInit: "0", // 待使用
};
const getters = {
  dicStoreInit: (state) => state.dicStoreInit,
};
const mutations = {
  SET_DIC_STORE(state, payload) {
    state.dicStoreInit = payload;
  },
};
const actions = {
  comAdminApiRequest({ commit, dispatch }, objData) {
    // console.log("comAdminApiRequest--->", objData.apiName);
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      $http
        .post(`/ms-insure/api/common/${objData.apiName}`, objData.apiParams, {
          json: true,
        })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          // reslove(data);
          if (data.code == "1") {
            reslove(data);
            if (objData.apiName === "getVarietyInfo") {
              dispatch("varietyInfo/initData", data, { root: true });
            }
            if (objData.apiName === "proposal") {
              commit("insert/SET_DOUBLE_VIDEO", data.needDouble || "0", {
                root: true,
              });
            }
            if (objData.apiName === "insertNoticeRead") {
              commit("page/SET_INDEX_GZ_FLAG", "1", {
                root: true,
              });
            }
          } else {
            if (objData.toast) {
              commit(
                "common/SET_FIELD_MEAN_CONT",
                {
                  label: `温馨提示`,
                  desc: data.msg,
                },
                { root: true }
              );
            }
          }
        })
        .catch((err) => {
          reject("err");
        })
        .finally(() => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  userSaveApi({ commit, rootGetters, rootState }, apiComParams) {
    return new Promise((reslove, reject) => {
      const bldUserId = rootGetters["insert/bldUserId"],
        proBldIdNo = rootGetters["insert/proBldIdNo"],
        appnts = rootGetters["insert/appnt"],
        apiBbrMult = rootGetters["insert/apiBbrMult"],
        conts = rootGetters["insert/cont"],
        msChannel = rootGetters["insert/msChannel"],
        totalPrem = rootGetters["insert/totalPrem"],
        XbHasModal = rootGetters["page/showXbModal"],
        insuType = rootGetters["page/isNetInsure"],
        showedRisks = rootGetters["risks/showedRisks"];

      let bbrlist = [];
      const len = apiBbrMult.length;
      for (let i = 0; i < len; i++) {
        const item = apiBbrMult[i];
        const obj = {
          ...JSON.parse(JSON.stringify(item)),
          risks: GetToRateData(showedRisks, i),
        };
        bbrlist.push(obj);
      }
      let params = {
        ...apiComParams,
        cont: conts,
        appnt: appnts,
        insureds: bbrlist,
        userId: bldUserId,
        goodsNo: proBldIdNo,
      };
      if (msChannel !=='') {
        params.msChannel = msChannel;
      }
      // 网销
      if (["1", "3"].includes(insuType)) {
        params.cont.totalPrem = totalPrem;
        if (XbHasModal) {
          params.insureds[0].renewFlag = conts.renewFlag || "-2";
        }
        if (insuType == "3") {
          const insureUrl = rootState.varietyInfo.continueUrl;
          params.continueUrl = `${insureUrl}&userId=${bldUserId}&goodsNo=${proBldIdNo}&orderNum=${params.orderNum}&chanl_type=3&ptype=netnair`;
        }
      }
      commit("updateLoad", true, { root: true });
      $http
        .post(`/ms-insure/api/common/saveCustomerInfo`, params, { json: true })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          reslove(data);
        })
        .catch((err) => {
          reject("err");
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  riskSaveApi({ commit, rootGetters }, apiComParams) {
    return new Promise((reslove, reject) => {
      const apiBbrMult = rootGetters["insert/apiBbrMult"],
        totalPrem = rootGetters["insert/totalPrem"],
        conts = rootGetters["insert/cont"],
        showXbCount = rootGetters["page/showXbCount"],
        showXbModal = rootGetters["page/showXbModal"],
        showedRisks = rootGetters["risks/showedRisks"];

      let bbrlist = [];
      const len = apiBbrMult.length;
      for (let i = 0; i < len; i++) {
        const item = apiBbrMult[i];
        const obj = {
          idNum: item.idNum,
          risks: GetToRateData(showedRisks, i),
        };
        bbrlist.push(obj);
      }
      let params = {
        ...apiComParams,
        cont: {
          ...conts,
          totalPrem: totalPrem,
        },
        insureds: bbrlist,
      };
      if (showXbCount || showXbModal) {
        params.insureds[0].renewFlag = conts.renewFlag || "-2";
      }
      commit("updateLoad", true, { root: true });
      $http
        .post(`/ms-insure/api/common/saveRisksInfo`, params, { json: true })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          reslove(data);
        })
        .catch((err) => {
          reject("err");
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  // 查询、导入客户信息
  rakeThroughApi({ commit }, apiComParams) {
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      $http
        .post(`/ms-insure/api/bilaiduo/getBldCustomerInfo`, apiComParams, {
          json: true,
        })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          reslove(data);
        })
        .catch((err) => {
          reject("err");
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  queryRechargeMoney({ commit, rootGetters }) {
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      const bldUserId = rootGetters["insert/bldUserId"];
      $http
        .post("ms-insure/api/bilaiduo/getRechargeMoney", {
          userId: bldUserId,
        })
        .then(({ data }) => {
          commit("updateLoad", false, { root: true });
          reslove(data);
        })
        .finally(() => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  getBillDetails({ commit }, apiParams) {
    // console.log("apis-getBillDetails");
    return new Promise((reslove, reject) => {
      $http
        .post(`/ms-insure/api/common/detail`, apiParams, { json: true })
        .then(({ data }) => {
          reslove(data);
        })
        .catch((err) => {
          reject("err");
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
  fileAdminUpload({ commit }, base64Pic) {
    return new Promise((reslove, reject) => {
      commit("updateLoad", true, { root: true });
      $http
        .post("/aliyun/oss/fileUpload", {
          data: base64Pic,
        })
        .then((data) => {
          reslove(data);
        })
        .catch((err) => {
          reject("err");
        })
        .finally(() => {
          commit("updateLoad", false, {
            root: true,
          });
        });
    });
  },
};
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
